<?php

global $WSC;

require_once('tcms_vpt_page_wp.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/category/tcms_category_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/ci_types/tcms_ci_normal_ci_dbc.class.php');


class TCMS_VPT_Product_Category_Content_WP extends TCMS_VPT_Page_WP {
	function __construct(TCMS_VPage_DBC $vpage, TCMS_VPage_Template_DBC $vpt) {
		parent::__construct($vpage, $vpt, 'tcms_vpt_product_category_content_page');
	}


	protected function default_event() {
		$dbc = $this->get_dbc();
// TODO - this have to return redirect to error page if category is not found
		$cat_hid = current(TCMS_VPage_System_Attribute_DBC::load_all_by_sa_export_name_vpage($dbc, 'category_hid', $this->get_vpage()->get_id()));
		if ($cat_hid) {
			$category = new TCMS_Category_DBC();
			$dbc = $this->get_dbc();
			if ($category->load_by_hid($dbc, $cat_hid->get_value())) {
				$this->export('category', $category);
				$vpages_arr_raw = TCMS_VPage_Category_DBC::select_vpage_and_vpage_template_for_cat($dbc, $category->get_id());

				$vpages_arr = $this->prepare_for_export($vpages_arr_raw);
				$this->export('vpages_arr', $vpages_arr);
			}
		}

		$settings_images = TCMS_Settings_Images_DBC::load_settings_flat($this->get_dbc());
		$this->export('t1_width', $settings_images['thumbnail1_size_x']);
		$this->export('t1_height', $settings_images['thumbnail1_size_y']);
		$this->export('t2_width', $settings_images['thumbnail2_size_x']);
		$this->export('t2_height', $settings_images['thumbnail2_size_y']);

		return parent::default_event();
	}


	public static function get_short_text_description(DB_Connection $dbc, $vpage_id) {
		tangra_if_not_int_throw_e($vpage_id);
		$page_system_attributes = TCMS_VPage_System_Attribute_DBC::load_all_for_vpage($dbc, $vpage_id);

		$ret = '';

		if ($page_system_attributes) {
			$cat_hid = current($page_system_attributes)->get_value();
			$category = new TCMS_Category_DBC();
			if ($category->load_by_hid($dbc, $cat_hid)) {
				$ret = $category->get_title();
			}
		}

		return $ret;
	}


	private function prepare_for_export($vpages_arr) {
		$dbc = $this->get_dbc();
		$hidden_inc = $this->_config->get_hidden_dir().'inc/';
		$ret = array();
		foreach($vpages_arr as $key => $vpage) {
			if ($vpage['wp_class_name'] == 'TCMS_VPT_Product_Page_WP') {
				$tmp_arr = $this->get_intro_arr($dbc, $vpage['vpage']);
				if ($tmp_arr) {
					$vpages_arr[$key]['text'] = $tmp_arr['text'];
					$vpages_arr[$key]['title'] = $tmp_arr['title'];
					$vpages_arr[$key]['image'] = $tmp_arr['image'];

					$ret[] = $vpages_arr[$key];
				}
			}
		}

		return $ret;
	}


	public function get_intro_arr(DB_Connection $dbc, $vpage_id) {
		tangra_if_not_int_throw_e($vpage_id);

		$ret = array();

		$intro_ci = new TCMS_VPage_CI_DBC();
		if ($intro_ci->load_ci_by_page_and_export_name($dbc, $vpage_id, 'intro')) {
			$title_sa = current(TCMS_VPage_System_Attribute_DBC::load_all_by_sa_export_name_vpage($dbc, 'title', $vpage_id));
			if ($title_sa) {
				$ci_normal = new TCMS_CI_Normal_CI_DBC();
				$ci_normal->load_by_id($dbc, $intro_ci->get_ci_id());


				$ret['text'] = $ci_normal->get_content()->get_value();
				$ret['title'] = $title_sa->get_value();

				$images = TCMS_VPT_Page_WP::load_images_for_ci($dbc, $intro_ci->get_ci_id());
				if ($images) {
					$ret['image'] = $images[0]->get_id();
				} else {
					$ret['image'] = '';
				}
			}
		}

		return $ret;
	}


	public function get_vpage_last_modified(DB_Connection $dbc = null) {
		if (!$dbc) {
			$dbc = $this->get_dbc();
		}

		$ret = '1970-01-01 00:00:00';

		$cat_hid = current(TCMS_VPage_System_Attribute_DBC::load_all_by_sa_export_name_vpage($dbc, 'category_hid', $this->get_vpage()->get_id()));

		if ($cat_hid) {
			$category = new TCMS_Category_DBC();

			if ($category->load_by_hid($dbc, $cat_hid->get_value())) {
				$vpage = $this->get_vpage();
				$current_last_modified = $vpage->get_last_modified();

				$newer_lm = TCMS_VPage_Category_DBC::find_vpage_newer_last_modified_for_cat($dbc, $category->get_id(), $current_last_modified);
				if ($newer_lm || $category->get_last_modified() > $this->get_vpage()->get_last_modified()) {
					if ($newer_lm > $category->get_last_modified()) {
						$ret = $newer_lm;
					} else {
						$ret = $category->get_last_modified();
					}
				} else {
					$ret = $current_last_modified;
				}
			}
		}

		return $ret;
	}
}